package com.example.yiwu;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.LogUtil;

public class LoginActivity extends Activity {
    private EditText mUsernameView;
    private EditText mPasswordView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
/**
 * *****************************************************************************************
 *    系统状态栏透明化代码
 */
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
/**
 * ********************************************************************************************
 */
        setContentView(R.layout.activity_login);

        mUsernameView = (EditText) findViewById(R.id.login_username);
        mPasswordView = (EditText) findViewById(R.id.login_password);

        TextView btnAboutYiwu = (TextView) findViewById(R.id.about_in_login) ;
        btnAboutYiwu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mainIntent = new Intent(LoginActivity.this, AboutActivity.class);
                startActivity(mainIntent);
            }
        });

        TextView btnLogToReg = (TextView) findViewById(R.id.login_to_register);
        btnLogToReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mainIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(mainIntent);
                LoginActivity.this.finish();
            }
        });

        Button mUsernameLoginButton = (Button) findViewById(R.id.login_button);
        mUsernameLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();
        LogUtil.log.d("TAG","start");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtil.log.d("TAG","restart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.log.d("TAG","resume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.log.d("TAG","pause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.log.d("TAG","destroy");
//        Intent mainIntent = new Intent(LoginActivity.this, ChoiceActivity.class);
//        startActivity(mainIntent);
//        LoginActivity.this.finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtil.log.d("TAG","STOP");
    }

    private void attemptLogin() {
        mUsernameView.setError(null);
        mPasswordView.setError(null);

        final String username = mUsernameView.getText().toString();
        final String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_invalid_password));
            focusView = mPasswordView;
            cancel = true;
        }

        if (TextUtils.isEmpty(username)) {
            mUsernameView.setError(getString(R.string.error_field_required));
            focusView = mUsernameView;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            AVUser.logInInBackground(username, password, new LogInCallback<AVUser>() {
                @Override
                public void done(AVUser avUser, AVException e) {
                    if (e == null) {
                        Toast.makeText(LoginActivity.this,"正在登录，请稍后", Toast.LENGTH_SHORT).show();
                        LoginActivity.this.finish();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    } else {
                        Toast.makeText(LoginActivity.this,"无法登录，请再次检查用户名密码及网络设置", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 6;
    }}
